package com.rom.update;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import androidx.core.app.NotificationCompat;

import java.io.File;
import java.io.IOException;

public class OtaUpgradeNotifyActivity extends Activity {
    private static final String DIALOG_TITLE = "系统即将升级，请确认是否立即升级？(%s秒)";
    private static final int SECONDS_DETERMINE = 11;
    private static final String TAG = OtaUpgradeNotifyActivity.class.getSimpleName();
    private static final String WAKELOCK_KEY = "UpdateAndReboot";
    private static WakeLock mWakeLock;
    CountDownTimer mCountDownTimer;
    /* access modifiers changed from: private */
    public TextView mTvTitle;
    private int secondRemain = 11;

    public OtaUpgradeNotifyActivity() {
        CountDownTimer r1 = new CountDownTimer(11000, 1000) {
            public void onTick(long millisUntilFinished) {
                OtaUpgradeNotifyActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        OtaUpgradeNotifyActivity.this.mTvTitle.setText(String.format(OtaUpgradeNotifyActivity.DIALOG_TITLE, new Object[]{Integer.valueOf(OtaUpgradeNotifyActivity.access$006(OtaUpgradeNotifyActivity.this))}));
                    }
                });
            }

            public void onFinish() {
                OtaUpgradeNotifyActivity.this.mTvTitle.setText(String.format(OtaUpgradeNotifyActivity.DIALOG_TITLE, new Object[]{"0"}));
                OtaUpgradeNotifyActivity.this.doOtaUpgrade();
            }
        };
        mCountDownTimer = r1;
    }

    static /* synthetic */ int access$006(OtaUpgradeNotifyActivity x0) {
        int i = x0.secondRemain - 1;
        x0.secondRemain = i;
        return i;
    }

    /* access modifiers changed from: protected */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recovery);
        initView();
    }

    private void initView() {
        this.mTvTitle = (TextView) findViewById(R.id.tv_title);
        this.mCountDownTimer.start();
        findViewById(R.id.tv_confirm).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                OtaUpgradeNotifyActivity.this.mCountDownTimer.cancel();
                OtaUpgradeNotifyActivity.this.doOtaUpgrade();
            }
        });
        findViewById(R.id.tv_delay).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                OtaUpgradeNotifyActivity.this.mCountDownTimer.cancel();
                OtaUpgradeNotifyActivity.this.delayNotifyUpgrade();
            }
        });
        mWakeLock = ((PowerManager) getSystemService("power")).newWakeLock(805306374, WAKELOCK_KEY);
    }

    /* access modifiers changed from: private */
    public void doOtaUpgrade() {
        //  File otaFile = new File("/data/media/0/update.zip");
        //File otaFile = new File("/sdcard/Download/update.zip");
       File otaFile = new File("/data/media/0/Download/update.zip");
        try {
            mWakeLock.acquire();
            CloudRecoverySystem.installPackage(this, otaFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /* access modifiers changed from: private */
    public void delayNotifyUpgrade() {
        PendingIntent restartIntent = PendingIntent.getActivity(this, 0, new Intent(this, OtaUpgradeNotifyActivity.class), 1073741824);
        AlarmManager mgr = (AlarmManager) getSystemService(NotificationCompat.CATEGORY_ALARM);
        if (VERSION.SDK_INT >= 23) {
            mgr.setExactAndAllowWhileIdle(0, System.currentTimeMillis() + 3600000, restartIntent);
        } else if (VERSION.SDK_INT >= 19) {
            mgr.setExact(0, System.currentTimeMillis() + 3600000, restartIntent);
        }
        finish();
    }
}

//public class OtaUpgradeNotifyActivity extends Activity {
//
//    private static final String TAG = OtaUpgradeNotifyActivity.class.getSimpleName();
//
//    private static final String DIALOG_TITLE = "系统即将升级，请确认是否立即升级？(%s秒)";
//
//    private static final int SECONDS_DETERMINE = 11;
//
//    private TextView mTvTitle;
//
//    private static final String WAKELOCK_KEY = "UpdateAndReboot";
//    private static PowerManager.WakeLock mWakeLock;
//
//    private int secondRemain = SECONDS_DETERMINE;
//    CountDownTimer mCountDownTimer = new CountDownTimer(SECONDS_DETERMINE * 1000, 1000L) {
//        @Override
//        public void onTick(final long millisUntilFinished) {
//            runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    mTvTitle.setText(String.format(DIALOG_TITLE, --secondRemain));
//                }
//            });
//        }
//
//        @Override
//        public void onFinish() {
//            mTvTitle.setText(String.format(DIALOG_TITLE, "0"));
//            doOtaUpgrade();
//        }
//    };
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_recovery);
//        initView();
//    }
//
//    private void initView() {
//        mTvTitle = findViewById(R.id.tv_title);
//        mCountDownTimer.start();
//
//        findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mCountDownTimer.cancel();
//                doOtaUpgrade();
//            }
//        });
//
//        findViewById(R.id.tv_delay).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mCountDownTimer.cancel();
//                delayNotifyUpgrade();
//            }
//        });
//
//        PowerManager powerManager = (PowerManager) this.getSystemService(this.POWER_SERVICE);
//        mWakeLock = powerManager.newWakeLock(
//                PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, WAKELOCK_KEY);
//    }
//
//    private void doOtaUpgrade() {
////        startActivity(new Intent(this, OtaUpgradingActivity.class));
////        finish();
//
//        Log.e(TAG, "doOtaUpgrade");
//        //        //File otaFile = new File("/sdcard/Download/update.zip");
//        File otaFile = new File("/data/media/0/Download/update.zip");
//        try {
//            mWakeLock.acquire();
//            CloudRecoverySystem.installPackage(this, otaFile);
//        } catch (IOException e) {
//             e.printStackTrace();
//        }
//    }
//
//    private void delayNotifyUpgrade() {
//        long delayMils = 60 * 60 * 1000L;
////        long delayMils = 5 * 1000L;
//        Intent intent = new Intent(this, OtaUpgradeNotifyActivity.class);
//        PendingIntent restartIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
//        AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
//            mgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delayMils, restartIntent);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
//            mgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delayMils, restartIntent);
//        }
//        finish();
//    }
//
//}

